Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
why nodemailer giving error here || UnhandledPromiseRejectionWarning || No recipients defined

i am new to js and development, making node mailer to send email after pushing payloads to db.

Error (node:8244) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Error: No recipients defined

My post controller for crating product

const mailer = require("../config/nodemailer");
module.exports.create = async (req, res) => {

  try {
  let product = new Product({
    name: req.body.name,
    description: req.body.description,
    isFeatured: req.body.isFeatured,
  });

    // await product.save();
    await mailer.contact(req, res); // node mailer
    // console.log("productCreated ==>>", product);
    // res.status(200).json(product);

    return res.send("Dummy :product created");
  } catch (error) {
    console.log("******Error While inserting data******", error);
    res.status(500).json({ message: error.message });
  }
};

node mailer controller

const nodemailer = require("nodemailer");

exports.contact = async (req, res) => {
//   var name = req.body.name;
//   var from = req.body.from;
//   var message = req.body.message;
//   var to = "jn";
  try {
    let transporter = nodemailer.createTransport({
      service: "gmail",
      host: "smtp.gmail.com",
      port: 587,
      secure: false, // true for 465, false for other ports
      auth: {
        user: "**********@gmail.com", 
        pass: "password",
      },
    });
    //
    var mailoptions = {
      from: "**********@gmail.com",
      to:"**********@gmail.com",
      subject: "Sending Email using Node.js",
      text: "That was easy!",
    };
    await transporter.sendMail({
      mailoptions,
      function(err, res) {
        if (err) {
          return res.status(200).json({
            message: err.message,
          });
        }
        return res.status(200).json({ message: "success" });
      },
    });
  } catch (error) {
    console.log(error);
    return res.json({ message: message });
  }
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Nodemailer expects an array or a comma separated list, you are using a plain string as to

to - Comma separated list or an array of recipients email addresses that will appear on the To: field

taken from the official documentation https://nodemailer.com/message/

about 4 years ago · Juan Pablo Isaza Relatório

0

Hi bro first import nodemailer

const nodemailer = require("nodemailer");

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  }
});

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'friend@yahoo.com',
  subject: 'Your subject',
  text: 'Your message!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

follow above mention steps and make sure that your sending mailid has no two step verification enabled.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda